package junghoAndI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class UpdateDog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField collar;
	private JTextField sno;
	private JTextField etc;
	
	private DogVO m = new DogVO();
	private DogDAO ddao = new DogDAO();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateDog dialog = new UpdateDog("0",  0, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UpdateDog(final String id, final int r, final JTable dogTable) {
		setTitle("강아지 정보 수정");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel txtCollar = new JLabel("목걸이번호");
		collar = new JTextField();
		collar.setColumns(10);
		JLabel txtSNO = new JLabel("담당보호소");
		sno = new JTextField();
		sno.setColumns(10);
		JLabel txtMercykilling = new JLabel("안락사여부");
		JLabel txtEtc = new JLabel("비고");
		etc = new JTextField();
		etc.setColumns(10);
		
		final JComboBox mercykillingCbx = new JComboBox();
		mercykillingCbx.setModel(new DefaultComboBoxModel(new String[] {"안락사check", "아니오", "예"}));
		
		final JComboBox snoCbx = new JComboBox();
		snoCbx.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				sno.setText((String)snoCbx.getSelectedItem());
			}
		});
		snoCbx.setModel(new DefaultComboBoxModel(new String[] {"한국동물구조관리협회", "카라(사)동물보호시민단체", "동물학대방지연합", "부산동물학대방지연합", "대전동물보호센터", "동물보호센터", "한국동물보호협회", "동물자유연대", "부산유기동물보호협회", "하늘아이", "동물사랑실천협회", "캐비클럽", "동물보호관리시스템", "(사)한국야생동물보호협회"}));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtCollar)
							.addGap(18)
							.addComponent(collar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtEtc)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(etc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtMercykilling)
							.addGap(18)
							.addComponent(mercykillingCbx, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtSNO)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(sno, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addComponent(snoCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(360, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtCollar)
						.addComponent(collar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(95)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtSNO)
						.addComponent(sno, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(snoCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(99)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtEtc)
						.addComponent(etc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtMercykilling)
						.addComponent(mercykillingCbx, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(37))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("수정");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DogVO m = new DogVO(id, null, null, null, null, null, collar.getText(), null, null, ddao.findSnoDog(sno.getText()), etc.getText(), (String)mercykillingCbx.getSelectedItem(), null);
						int cnt = new DogDAO().updateDog(m);
						if (cnt > 0) {
							JOptionPane.showMessageDialog(null, "수정완료!");
							dogTable.setValueAt(sno.getText(), r, 6);
							dogTable.setValueAt(mercykillingCbx.getSelectedItem(), r, 7);
							dogTable.repaint();
							dispose();
						}
					}
				});
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
			{
				JButton cancelBtn = new JButton("취소");
				cancelBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelBtn.setActionCommand("Cancel");
				buttonPane.add(cancelBtn);
			}
		}
		DogVO dg = new DogDAO().selectOneDog(id);
		collar.setText(dg.getCollar());
		sno.setText(dg.getSno());
		etc.setText(dg.getEtc());
		mercykillingCbx.setSelectedItem(dg.getMercykilling());
		
		
		
	}
}



